import { query } from '$lib/config/db';

const usersModule = {
  getById: async (id) => {
    const sql = 'SELECT * FROM users WHERE id = $1';
    const params = [id];
    const rows = await query(sql, params);
    return rows[0];
  },
  getByUsername: async (username) => {
    const sql = 'SELECT * FROM users WHERE username = $1';
    const params = [username];
    const rows = await query(sql, params);
    return rows[0];
  },
  getAll: async () => {
    const sql = 'SELECT * FROM users';
    return await query(sql);
  },
  createUser: async ({username, password}) => {
    const sql = 'INSERT INTO users (username, password) VALUES ($1, $2) RETURNING *';
    const params = [username, password];
    const rows = await query(sql, params);
    return rows[0];
  }
}

export default usersModule;